package cz.byteworks.android.myway.location;

import java.util.Date;
import java.util.Timer;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.LocationListener;
import android.location.LocationManager;
import cz.byteworks.android.myway.activities.DataActivity;
import cz.byteworks.android.myway.activities.R;
import cz.byteworks.android.myway.db.DataHelper;
import cz.byteworks.android.myway.util.AutoButtonTextTask;
import cz.byteworks.android.myway.util.NowButtonTextTask;

public class AutoLocator {

	private static final int MYWAY_TRACKER_NOTIF_ID = 846515493;
	private static Timer timer;
	private static LocationListener locationListener;
	private static boolean locating;

	public synchronized static void startAutolocation(Context ctx, DataHelper dataHelper, int period) {
		LocTimerTask timerTask = new LocTimerTask(ctx, dataHelper, period);
		timer = new Timer();
		timer.schedule(timerTask, new Date(), period * 1000);
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
		int icon = R.drawable.ic_stat_notification;
		CharSequence tickerText = "Tracking is enabled";
		long when = System.currentTimeMillis();

		Notification notification = new Notification(icon, tickerText, when);
		CharSequence contentTitle = "MyWay Tracker";
		CharSequence contentText = "Tracking is enabled";
		Intent notificationIntent = new Intent(ctx, DataActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);

		notification.setLatestEventInfo(ctx, contentTitle, contentText, contentIntent);
		notification.flags = Notification.FLAG_NO_CLEAR;
		mNotificationManager.notify(MYWAY_TRACKER_NOTIF_ID, notification);
		scheduleButtonTextChange(ctx);
	}

	public static synchronized void stopAutolocation(Context ctx) {
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
		mNotificationManager.cancel(MYWAY_TRACKER_NOTIF_ID);
		scheduleButtonTextChange(ctx);
		stopLocating(ctx);
	}

	public synchronized static boolean isAutolocationRunning() {
		return timer != null;
	}

	public synchronized static void stopLocating(Context ctx) {
		AutoLocator.setLocating(false);
		getLocationManager(ctx).removeUpdates(locationListener);
		scheduleNowButtonTextChange(ctx);
	}

	public static LocationManager getLocationManager(Context ctx) {
		return (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
	}

	public static void setLocationListener(LocationListener locationListener) {
		AutoLocator.locationListener = locationListener;
	}

	public static void setLocating(boolean b) {
		locating = b;
	}

	public synchronized static boolean isLocating() {
		return locating;
	}
	
	private static void scheduleButtonTextChange(Context ctx) {
		Timer timer = new Timer();
		AutoButtonTextTask task = new AutoButtonTextTask(ctx, timer);
		timer.schedule(task, new Date(), 100);
	}
	
	private static void scheduleNowButtonTextChange(Context ctx) {
		Timer timer = new Timer();
		NowButtonTextTask task = new NowButtonTextTask(ctx, timer);
		timer.schedule(task, new Date(), 100);
	}

}
